package logic

import (
	"context"
	"vip-shop-hub/models/mysql"

	"vip-shop-hub/viprpc/internal/svc"
	"vip-shop-hub/viprpc/viprpc"

	"github.com/pkg/errors"
	"github.com/zeromicro/go-zero/core/logx"
)

type UserPerfectLogic struct {
	ctx    context.Context
	svcCtx *svc.ServiceContext
	logx.Logger
}

func NewUserPerfectLogic(ctx context.Context, svcCtx *svc.ServiceContext) *UserPerfectLogic {
	return &UserPerfectLogic{
		ctx:    ctx,
		svcCtx: svcCtx,
		Logger: logx.WithContext(ctx),
	}
}

func (l *UserPerfectLogic) UserPerfect(in *viprpc.UserPerfectRequest) (*viprpc.UserPerfectResponse, error) {
	// todo: add your logic here and delete this line
	var user mysql.User
	if err := user.FIndUserBY(mysql.DB, in.UserId); err != nil {
		return nil, errors.New("当前用户不存在")
	}
	user.NickName = in.NickName
	user.HeadImage = in.HandImage
	user.Address = in.Address
	user.Age = in.Age
	user.Email = in.Email

	if err := user.UpdateUser(mysql.DB); err != nil {
		return nil, errors.New("完善信息失败 ")
	}

	return &viprpc.UserPerfectResponse{
		UserId: int64(user.ID),
	}, nil
}
